package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 朱峰
 * @date 2022-1-29 11:54
 */
public class 全排列_46 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Deque<Integer> trace = new LinkedList<>();
        dfs(nums, trace, list);
        return list;
    }

    private void dfs(int[] nums, Deque<Integer> trace, List<List<Integer>> list) {
        if (nums.length == trace.size()) {
            list.add(new LinkedList<>(trace));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (trace.contains(nums[i])) {
                continue;
            }
            trace.add(nums[i]);
            dfs(nums, trace, list);
            trace.removeLast();
        }
    }

    @Test
    public void testPermute () {
        int[] nums = {1};
        List<List<Integer>> permute = permute(nums);
        System.out.println(permute);
    }
}
